<?php
class misc
{
    const BIT_OF_CHAR=8;
    const BIT_OF_BYTE=8;
    const CRLF="\r\n";

    const NAME_SIZE=30;
    const LONG_NAME_SIZE=200;
    const DATE_SIZE=8;
    const TIME_SIZE=6;
    const TIMESTAMP_SIZE=14;
    const ZIP_CODE_SIZE=6;
    const IP_SIZE=15;

    public static function name_is_valid($name, $len=self::NAME_SIZE)
    {/*{{{*/
        $n=trim($name);
        if(empty($n) || strlen($n) > $len)
            return false;

        if(mb_detect_encoding($n, 'UTF-8') != 'UTF-8')
            return false;

        if(preg_match('/["$%&\'\\\`]/', $n))
            return false;

        return true;
    }/*}}}*/

    public static function email_is_valid($email)
    {/*{{{*/
        $e=trim($email);
        if(empty($e) || strlen($e) > self::NAME_SIZE*2)
            return false;

        $arr=split('@', $e);
        if(count($arr) != 2)
            return false;
        $user=$arr[0];
        $host=$arr[1];
        if(preg_match('/[^a-zA-Z0-9\.-_]/', $user) ||
                preg_match('/[^a-zA-Z0-9\.-_]/', $host))
            return false;

        return true;
    }/*}}}*/

    public static function identity_is_valid($id)
    {
        return true;
    }

    public static function date_is_valid($date)
    {/*{{{*/
        if(strlen($date) != self::DATE_SIZE)
            return false;
        $year=intval(substr($date, 0, 4));
        $month=intval(substr($date, 4, 2));
        $day=intval(substr($date, 6, 2));
        if($year <= 0 || $month <= 0 || $month > 12 ||
                $day <=0 ||
                $day > cal_days_in_month(CAL_GREGORIAN, $month, $year))
            return false;
        return true;
    }/*}}}*/

    public static function time_is_valid($time)
    {/*{{{*/
        if(strlen($time) != self::TIME_SIZE)
            return false;
        $hour=substr($time, 0, 2);
        $min=substr($time, 2, 2);
        $sec=substr($time, 4, 2);
        if($hour < 0 || $hour > 24 ||
                $min < 0 || $min > 59 ||
                $sec < 0 || $sec > 59)
            return false;
        return true;
    }/*}}}*/

    public static function timestamp_is_valid($ts)
    {/*{{{*/
        if(strlen($ts) != self::TIMESTAMP_SIZE)
            return false;
        $date=substr($ts, 0, self::DATE_SIZE);
        $time=substr($ts, self::DATE_SIZE, self::TIME_SIZE);
        return self::check_date($date) && self::check_time($time);
    }/*}}}*/

    public static function change_date($date, $days)
    {/*{{{*/
        $yyyy=intval(substr($date, 0, 4));
        $mm=intval(substr($date, 4, 2));
        $dd=intval(substr($date, 6, 2));
        $day_count=cal_to_jd(CAL_GREGORIAN, $mm, $dd, $yyyy);
        $day_count+=$days;
        $arr=cal_from_jd($day_count, CAL_GREGORIAN);
        return sprintf("%04d%02d%02d",
                $arr['year'], $arr['month'], $arr['day']);
    }/*}}}*/

    public static function trim_not_empty($msg)
    {/*{{{*/
        $s=trim($msg);
        if(empty($s)) $s=' ';
        return $s;
    }/*}}}*/

    public static function rtrim_not_empty($msg)
    {/*{{{*/
        $s=rtrim($msg);
        if(empty($s)) $s=' ';
        return $s;
    }/*}}}*/
}
?>
